package test;

import java.util.ArrayList;
import java.util.List;

public class DataProcessor {

    public static void main(String[] args) {
        int[] data = {4, 12, 18, 10, 4
                , 12, 19, 13, 4
                , 13, 19, 13, 4

                , 10, 16, 10, 4
                , 12, 16, 11, 4
                , 10, 16, 10, 4};
        List<Double> result = processCycles(data);

        // 输出每个周期组的最大值百分比
        for (Double maxPercentage : result) {
            System.out.println(maxPercentage);
        }
    }

    public static List<Double> processCycles(int[] data) {
        List<List<Integer>> cycles = new ArrayList<>();
        List<Integer> currentCycle = new ArrayList<>();

        for (int num : data) {
            if (num == 4 && !currentCycle.isEmpty()) {
                // 将当前周期添加到周期列表中
                cycles.add(new ArrayList<>(currentCycle));
                currentCycle.clear();
                currentCycle.add(4);
            } else if (currentCycle.isEmpty() && num == 4) {
                currentCycle.add(4);
            } else if (!currentCycle.isEmpty()) {
                if (num >= currentCycle.get(currentCycle.size() - 1)) {
                    currentCycle.add(num);
                }
            }
        }

        // 如果当前周期不为空且周期数少于三个，则将当前周期添加到周期列表中
        if (!currentCycle.isEmpty() && cycles.size() < 3) {
            cycles.add(currentCycle);
        }

        // 计算每三个周期的最大值并转换为百分比
        final int minVal = 4;
        final int maxVal = 19;

        List<Double> maxPercentages = new ArrayList<>();
        for (int i = 0; i < cycles.size(); i += 3) {
            int maxValue = Integer.MIN_VALUE;
            for (int j = i; j < Math.min(i + 3, cycles.size()); j++) {
                for (int num : cycles.get(j)) {
                    if (num > maxValue) {
                        maxValue = num;
                    }
                }
            }
            double maxPercentage = ((double) (maxValue - minVal) / (maxVal - minVal)) * 100;
            maxPercentages.add(maxPercentage);
        }

        return maxPercentages;
    }
}